package com.sogoodsoft.web.crm;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.entity.crm.Smstemplate;
import com.sogoodsoft.service.account.AccountManager;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.service.crm.SmstemplateService;
import com.sogoodsoft.web.BaseAction;

@Results({ @Result(name = "selectTemplate", location = "/WEB-INF/content/crm/selectTemplate.jsp") })
public class SmstemplateAction extends BaseAction < Smstemplate, Long > {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private SmstemplateService service;

    /** 常用的系统类型和分类service **/
    @Autowired
    protected SysClassService sysClassService;

    /** 常用的用户service **/
    @Autowired
    protected AccountManager accountManager;

    @SuppressWarnings("rawtypes")
    @Override
    protected AbstractService getService() {
        return service;
    }

    /*
     * onSave()方法，会在save.Action前调用 SysClass类型信息，action中不会自动载入， 需手动按页面传入的ID载入类型，重新关联到entity中，才能正常保存
     */
    public void onSave() {
        // 设置分类
        Long cateid = getEntity().getCategory();
        if (cateid != null && entity.getSysClass() == null) {
            SysClass sysClass = sysClassService.get(cateid);
            getEntity().setSysClass(sysClass);
        }

        // 设置更新时间
        getEntity().setUpdatedate(new java.util.Date());
    }

    public String selectTemplate() throws Exception {
        this.list();
        return "selectTemplate";
    }

    public String ajaxTemplate() throws Exception {
        prepareModel();
        Struts2Utils.getResponse().setCharacterEncoding("UTF-8");
        Struts2Utils.getResponse().setContentType("text/plain;charset=UTF-8");
        Struts2Utils.getResponse().getWriter().print(entity.getTemplate());
        return null;
    }

}
